package com.primestone.cim.executeenddevicecontrols.async;


import java.io.Serializable;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.xml.bind.JAXBException;

import com.primestone.cim.executeenddevicecontrols.QueueMessage;
import com.primestone.cim.util.StringUtil;
import com.primestone.cim.util.XMLUtil;


@Stateless( name = StringUtil.QUEUEUPEXECEDC_NAME )
@TransactionManagement( TransactionManagementType.BEAN )
public class QueueUpBean implements QueueUpLocal {
    
	// pasar esta inyeccion a la impl delws, tambien la session y el producer
    @Resource(mappedName = StringUtil.CONN_FACTORY_JNDI_NAME)
    private ConnectionFactory connectionFactory;

    @Resource(mappedName=StringUtil.DESTINATION_EXEC_EDC_QUEUE)
    private Queue queue;
    
    
    static {
    	
    }
    
    public QueueUpBean() {
    	
    }
    
    /**
     *
     * @param message
     * @throws QueueException
     */
    public void receiveQueueMessage( Serializable message ) throws Exception {        
        try {
            System.out.println("---- En el ejb que coloca el request");
            if (message instanceof QueueMessage) {
            	putRequest((QueueMessage)message);
            	System.out.println("---- El ejb ha colocado el request");
            } else {
            	System.out.println("---- Ignoring message.");
            }
        } catch( JMSException e ) {
            System.out.println("---- Excepcion dequeueing: "+e);
            //ResponseMessage rm = new ResponseMessage();
            //rm.setMessage( Constants.Exceptions.QUEUE_EXC_MSG, e );
            throw new Exception( e );
        } catch (Throwable e) {
            System.out.println("---- Fundamental Excepcion enqueueing: "+e);
            e.printStackTrace();
            //ResponseMessage rm = new ResponseMessage();
            //rm.setMessage( Constants.Exceptions.QUEUE_EXC_MSG, e );
            throw new Exception( e );
        }
    }
    
    /**
     *
     * @param message
     * @throws JMSException
     * @throws JAXBException 
     */
    private void putRequest( QueueMessage message ) throws JMSException, JAXBException {
        Connection conn = null;
        Session session = null;
        MessageProducer producer = null;
        try {
            conn = connectionFactory.createConnection();
            session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(queue);
            
            BytesMessage bytesMessage = session.createBytesMessage();
            bytesMessage.writeBytes(XMLUtil.marshall(message));
            
            System.out.println("---- message.getHeader().value.getUser(): "+message.getHeader().value.getUser());
            bytesMessage.setStringProperty("user", message.getHeader().value.getUser().getUserID());
             
            producer.send(bytesMessage);
            System.out.println("---- Mensaje ha sido enviado!!");
        } finally {
            if( session != null ) {
                session.close();
            }
            if( conn != null ) {
                conn.close();
            }
        }
    }
    
}
